package org.br.erp.base.service.impl;

import org.br.erp.base.entity.Scmpayterms;
import org.br.erp.base.entity.Scmpaytermsitem;
import org.br.erp.base.mapper.ScmpaytermsitemMapper;
import org.br.erp.base.mapper.ScmpaytermsMapper;
import org.br.erp.base.service.IScmpaytermsService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 付款条款
 * @Author: jeecg-boot
 * @Date:   2023-01-04
 * @Version: V1.0
 */
@Service
public class ScmpaytermsServiceImpl extends ServiceImpl<ScmpaytermsMapper, Scmpayterms> implements IScmpaytermsService {

	@Autowired
	private ScmpaytermsMapper scmpaytermsMapper;
	@Autowired
	private ScmpaytermsitemMapper scmpaytermsitemMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(Scmpayterms scmpayterms, List<Scmpaytermsitem> scmpaytermsitemList) {
		scmpaytermsMapper.insert(scmpayterms);
		if(scmpaytermsitemList!=null && scmpaytermsitemList.size()>0) {
			for(Scmpaytermsitem entity:scmpaytermsitemList) {
				//外键设置
				entity.setParentid(scmpayterms.getId());
				scmpaytermsitemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(Scmpayterms scmpayterms,List<Scmpaytermsitem> scmpaytermsitemList) {
		scmpaytermsMapper.updateById(scmpayterms);
		
		//1.先删除子表数据
		scmpaytermsitemMapper.deleteByMainId(scmpayterms.getId());
		
		//2.子表数据重新插入
		if(scmpaytermsitemList!=null && scmpaytermsitemList.size()>0) {
			for(Scmpaytermsitem entity:scmpaytermsitemList) {
				//外键设置
				entity.setParentid(scmpayterms.getId());
				scmpaytermsitemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		scmpaytermsitemMapper.deleteByMainId(id);
		scmpaytermsMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			scmpaytermsitemMapper.deleteByMainId(id.toString());
			scmpaytermsMapper.deleteById(id);
		}
	}
	
}
